import { Injectable } from '@angular/core';
import { BaseService } from '@core/routes-services/base.service';
import { CacheService } from '@core/routes-services/cache.service';
import { HttpClient } from '@angular/common/http';

/**
 * 渠道 服务
 */
@Injectable()
export class AgentService extends BaseService {


  constructor(http: HttpClient, cacheService: CacheService) {
    super(http, cacheService);
  }

  base_url: any = 'api/agents';

  getCitiesData() {
    let that = this;
    return this.http.get('./assets/data/city-data.json')
      .toPromise()
      .then(response => {
        return response;
      })
      .catch(err => {
        return Promise.reject(err);
      });

  }

  /**
   * 分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  pagination(page: any, keyword: any, levelId: any, parentId: any, districtId: any): Promise<any> {
    const url = this.base_url + '/pagination';
    if (keyword) {
      page['_filter_like_keyword'] = keyword;
    }
    if (levelId) {
      page['_filter_eq_levelId'] = levelId;
    }
    if (parentId) {
      page['_filter_eq_parentId'] = parentId;
    }
    if (districtId) {
      page['_filter_eq_districtId'] = districtId;
    }
    return this.post(url, page);
  }

  /**
   * 修改
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  saveOrUpdateEntity(id: any, entity: any): Promise<any> {
    if (id) {
      const url = this.base_url + '/' + id;
      return this.put(url, entity);
    } else {
      const url = this.base_url;
      return this.post(url, entity);
    }
  }

  /**
   * 充值余额
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  recharge(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/pay/' + id;
    return this.put(url, entity);
  }

  /**
   * 渠道申请充值通过进行充值
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  payApplication(id: any, applicationId: any, entity: any): Promise<any> {
    const url = this.base_url + '/pay/' + id + '/application/' + applicationId;
    return this.put(url, entity);
  }

  /**
   * 重新设置渠道密码
   *
   * @param id 渠道id
   * @param entity
   * @returns {Promise<any>}
   */
  setPassword(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/actions/' + id + '/resetPassword';
    return this.put(url, entity);
  }

  /**
   * 渠道申请充值 不通过
   *
   * @param id 申请充值id
   * @param entity
   * @returns {Promise<any>}
   */
  auditing(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/reject/application/' + id;
    return this.put(url, entity);
  }

  /**
   * 渠道直接升级到市级渠道商
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  upgrade(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/upgrade/' + id;
    return this.put(url, entity);
  }

  /**
   * 后台管理修改渠道级别
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  updateLevel(id: any, levelId: any): Promise<any> {
    const url = this.base_url + '/actions/update/' + id + '/level/' + levelId;
    return this.put(url, {});
  }

  /**
   * 独代区域设置
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  setCityArea(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/actions/area/' + id;
    return this.put(url, entity);
  }

  /**
   * 获取查看详情
   *
   * @param id
   * @returns {Promise<any>}
   */
  detail(id: any): Promise<any> {
    const url = this.base_url + '/' + id;
    return this.get(url);
  }

  /**
   * 删除
   *
   * @param ids
   * @returns {Promise<any>}
   */
  deleteEntity(ids): Promise<any> {
    const url = this.base_url + '/';
    return this.delete(url, ids);
  }

  /**
   * 获取全部
   *
   * @returns {Promise<any>}
   */
  getAll(): Promise<any> {
    const url = this.base_url + '/all';
    return this.get(url);
  }


  /*------------------------招市级渠道商begin---------------------------------------*/

  /**
   * 分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  relatePagination(page: any, keyword: any, type: any): Promise<any> {
    const url = 'api/agent_level_relates/pagination';
    if (keyword) {
      page['_filter_like_keyword'] = keyword;
    }
    if (type) {
      page['_filter_eq_type'] = type;
    }
    return this.post(url, page);
  }

  /**
   * 保存、 修改关联关系
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  saveOrUpdateRelate(id: any, entity: any): Promise<any> {
    if (id) {
      console.log('暂无修改方法');
      // const url =  'api/agent_level_relates/' + id;
      // return this.put(url, entity);
    } else {
      const url = 'api/agent_level_relates/save';
      return this.post(url, entity);
    }
  }

  /**
   * 根据级别id获取全部
   *
   * @returns {Promise<any>}
   */
  getAllBylevel(level): Promise<any> {
    const url = this.base_url + '/all/' + level;
    return this.get(url);
  }
}
